package com.xj.demo.unknow;

import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

public class WeakHashMapTest {
    public static void main(String[] args) {
        WeakHashMapTest test = new WeakHashMapTest();
        test.init();
        test.weakHashMapGC();
        test.hashMapGC();
    }
    Map<String,Object> wkMap = new WeakHashMap<>();
    Map<String,Object> map = new HashMap<>();

    public void init(){
        String str1 = new String("obj1");
        String str2 = new String("obj2");
        String str3 = new String("obj3");
        String str4 = new String("obj4");
        wkMap.put(str1, "value1");
        wkMap.put(str2, "value2");
        map.put(str3, "value3");
        map.put(str4, "value4");
    }
    public void weakHashMapGC(){
        System.out.println("weakHashMap GC前");
        for (String key:wkMap.keySet()){
            System.out.println(key + "--->" + wkMap.get(key));
        }
        System.gc();
        System.out.println("weakHashMap GC后");
        for (String key:wkMap.keySet()){
            System.out.println(key + "--->" + wkMap.get(key));
        }
    }
    public void hashMapGC(){
        System.out.println("hashMap GC前");
        for (String key:map.keySet()){
            System.out.println(key + "--->" + map.get(key));
        }
        System.gc();
        System.out.println("hashMap GC后");
        for (String key:map.keySet()){
            System.out.println(key + "--->" + map.get(key));
        }
    }
}
